package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.UserLoginBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.PromptBiz;
import com.mentaleye.biz.UserLoginBiz;
import com.mentaleye.dal.model.Dictionary;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.shiro.Constants;
import com.mentaleye.shiro.RedisCache;
import com.mentaleye.shiro.RedisKeyEnum;
import com.mentaleye.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userLoginController")
public class UserLoginController {
	@Autowired private UserLoginBiz loginBiz;
	
	@Autowired private DictionaryBiz dictionaryBiz;
	
	@Autowired private RedisCache<String, Object> redisCache;

	@Autowired PromptBiz promptBiz;

	@RequestMapping("/toLogin")
	public ModelAndView toLogin(){
		List<Dictionary> dictionaryList = this.dictionaryBiz.getListByParent(DictionaryEnums.PSERSON_TYPE.getCode());
		ModelAndView view=new ModelAndView("/login");
		view.addObject("loginTypeList", dictionaryList);
		return view;
	}
	
	@RequestMapping("/toEdit")
	public ModelAndView toEdit(@RequestParam String loginName){
		ModelAndView view=new ModelAndView("ss/userLogin/loginEdit");
		UserLoginBean loginBean =(UserLoginBean) redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_SESSION, loginName));
		view.addObject("userLoginBean", this.loginBiz.getBeanBySelective(loginBean));
		return view;
	}

	@RequestMapping("/permNo")
	public String permNo(){
		return "/permNo";
	}
	
	@RequestMapping("/checkLogin")
	@ResponseBody
	public String checkLogin(@RequestBody UserLoginBean dataBean){
		CommonResult result = this.loginBiz.checkLogin(dataBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/checkLoginName")
	@ResponseBody
	public String checkLoginName(@RequestBody UserLoginBean dataBean){
		CommonResult result = this.loginBiz.checkLoginName(dataBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/toIndex")
	public ModelAndView toIndex(){
		ModelAndView view=new ModelAndView();
		CommonResult result = this.loginBiz.toIndex();
		List<Map<String, Object>> countMap= promptBiz.getCount();
		if (result.getIsSuccess()) {
			view.setViewName("/index");
			view.addObject("loginList", JSONObject.toJSON(result.getData()));
			view.addObject("countMap", countMap);
		} else {
			view.setViewName("redirect:/toLogin");
		}
		return view;
	}
	
	@RequestMapping("/loginOut")
	public String loginOut() {
		this.loginBiz.loginOut();
		return "redirect:/toLogin.html";
	}
	
	@RequestMapping("/loginEdit")
	@ResponseBody
	public String loginEdit(UserLoginBean dataBean){
		CommonResult result = this.loginBiz.updateRecordSelective(dataBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/building")
    public String building() {
        return "error/404";
    }

    @RequestMapping("/error")
    public String error() {
        return "error/500";
    }
}
